package com.cg;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;

import java.io.IOException;
import java.net.URI;

public class MyTest {
    public static void main(String[] args) throws IOException {
//        httpClient();
        restTemplate();
    }

    public static void httpClient() throws IOException {
        User user = User.builder().username("zhangsan").age(15).build();
        HttpPost httpPost = new HttpPost("http://localhost:8080/call");
        StringEntity stringEntity = new StringEntity(user.toString());
        stringEntity.setContentType("application/json");
        stringEntity.setContentEncoding("UTF-8");
        httpPost.setEntity(stringEntity);

//        httpPost.setHeader();
        CloseableHttpClient client = HttpClients.createDefault();
        HttpResponse httpResponse = client.execute(httpPost);
        System.out.println(httpResponse.getEntity().getContent());
    }

    public static void restTemplate() {
        RestTemplate restTemplate = new RestTemplate();
        UriComponents uriComponents = UriComponentsBuilder.fromUriString("http://localhost:8080/call2/{username}")
                .build()
                .expand("lisi")
                .encode();
        URI uri = uriComponents.toUri();
        ResponseEntity<String> forEntity = restTemplate.getForEntity(uri, String.class);
        System.out.println(forEntity.getBody());
    }
}
